package com.example.demo.repository;

import com.example.demo.entity.Order;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

/**
 * 订单数据访问接口
 */
@Repository
public interface OrderRepository extends JpaRepository<Order, Long> {

    /**
     * 根据订单号查询最新版本
     */
    Optional<Order> findFirstByOrderNoOrderByUpdateTimestampDesc(String orderNo);

    /**
     * 查询订单的所有历史版本，按时间排序
     */
    @Query("SELECT o FROM Order o WHERE o.orderNo = :orderNo ORDER BY o.updateTimestamp ASC")
    List<Order> findAllVersionsByOrderNo(@Param("orderNo") String orderNo);
}
